﻿using Common;
using DataModel;
using LinqToDB;
using ProjectManager.User;
using ProjectManager.User.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManagerSys
{
    /// <summary>
    /// 日志或者流程操作
    /// </summary>
    public class LogDataOperation
    {
        
        /// <summary>
        /// 任务操作流程入库
        /// </summary>
        /// <param name="id">关联数据id</param>
        /// <param name="content">操作内容</param>
        /// <param name="detail">详细内容</param>
        /// <param name="oldcontent">更改前数据</param>
        /// <param name="newcontent">更改后数据</param>
        public async void TaskFlowLog(string id="",string content="",string detail="",string oldcontent="",string newcontent="")
        {
            using (var con = Data.DB.GetConnection())
            {
                //日志入库
                MANAGER_TASKFLOWLOG oFlow = new MANAGER_TASKFLOWLOG();
                oFlow.ID = Guid.NewGuid().ToString();
                oFlow.ACTIVE = 1;
                oFlow.CREATETIME = DateTime.Now;
                oFlow.UPDATETIME = DateTime.Now;
                oFlow.TASKID = id;
                oFlow.CONTENT = content;
                oFlow.DETAIL = detail;
                oFlow.OLDCONTENT = oldcontent;
                oFlow.NEWCONTENT = newcontent;
                AuthInfo authinfo = Logins.Authorize();
                oFlow.CREATENAME = authinfo.userid;
                oFlow.DEPARTCODE = authinfo.departid;
                oFlow.REALNAME = authinfo.realname;
                await con.InsertAsync(oFlow);
            }
                
        }

        /// <summary>
        /// 系统日志入库
        /// </summary>
        /// <param name="id">关联数据id</param>
        /// <param name="content">操作</param>
        /// <param name="detail">详细内容</param>
        /// <param name="tablename">表名</param>
        public async void SysLog(string id = "", string content = "", string detail = "", string tablename="")
        {
            using (var con = Data.DB.GetConnection())
            {
                AuthInfo authinfo = Logins.Authorize();
                //日志入库
                BJSYS_LOG log = new BJSYS_LOG();
                log.ID = Guid.NewGuid().ToString();
                log.ACTIVE = 1;
                log.CREATETIME = DateTime.Now;
                log.UPDATETIME = DateTime.Now;
                log.CREATENAME = authinfo.userid;
                log.TABLENAME = tablename;
                log.OPERATION = content;
                log.DETAIL = detail;
                log.RESOURCEID = id;
                await con.InsertAsync(log);
            }

        }

        /// <summary>
        /// 消息入库
        /// </summary>
        /// <param name="type">消息类型</param>
        /// <param name="receiveid">消息接收人id</param>
        /// <param name="id">关联信息id</param>
        /// <param name="detail">详细内容</param>
        /// <param name="tablename">关联表名</param>
        public async void noticeLog(int type =1,string receiveid="",string id = "", string detail = "", string tablename = "")
        {
            using (var con = Data.DB.GetConnection())
            {
                if (!string.IsNullOrEmpty(receiveid))
                {
                    string[] arr = receiveid.Split(new string[] { ","},StringSplitOptions.RemoveEmptyEntries);
                    arr = arr.Distinct().ToArray(); //数组去重
                    foreach(var item in arr)
                    {
                        AuthInfo authinfo = Logins.Authorize();
                        //日志入库
                        MANAGER_NOTICE oNotice = new MANAGER_NOTICE();
                        oNotice.ID = Guid.NewGuid().ToString();
                        oNotice.ACTIVE = 1;
                        oNotice.CREATETIME = DateTime.Now;
                        oNotice.UPDATETIME = DateTime.Now;
                        oNotice.CREATEUSERID = authinfo.userid;
                        oNotice.CREATENAME = authinfo.realname;
                        oNotice.TABLENAME = tablename;
                        oNotice.DETAIL = detail;
                        oNotice.LINKID = id;
                        oNotice.TYPE = type;
                        oNotice.RECEIVEUSERID = item;
                        oNotice.ISREAD = 0;
                        await con.InsertAsync(oNotice);
                    }
                }
                
            }

        }
    }
}
